package com.example.hidesms;

import com.example.hidesms.HomePageFragment.HomeListener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MenuFacebookFragment extends Fragment {
	private final static String		TAG			= MenuFacebookFragment.class.getSimpleName();
	private final static String		ARGUMENTS	= TAG + ":bundle_arguments";
	private RelativeLayout			blockSms, blockIncoming, blockOutgoing, blockCoolSms;
	private TextView				personalSearch;
	// private MenuFacebookRetain menuFacebookRetain;
	TextView						txtFullName;
	TextView						txtGold;
	private FillUsernameReceiver	completeReceiver;
	private MenuFacebookRetain retain;
	
	public interface MenuListener{
		public void addListBlock();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.menu_fb_layout, null);
		init();
		getUIElementFromLayout(view);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		completeReceiver = new FillUsernameReceiver();
		getActivity().registerReceiver(completeReceiver, new IntentFilter("vcc.com.muachung.fillusername"));
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onDestroy() {
		if (completeReceiver != null) {
			getActivity().unregisterReceiver(completeReceiver);
		}
		super.onDestroy();
	}
	private void init(){
		if (retain == null) {
			retain = (MenuFacebookRetain) getArguments().get(ARGUMENTS);
		}
	}

	private void getUIElementFromLayout(View view) {
		blockSms = (RelativeLayout) view.findViewById(R.id.menu_block_sms);
		blockIncoming = (RelativeLayout) view.findViewById(R.id.menu_block_in_coming);
		blockOutgoing= (RelativeLayout) view.findViewById(R.id.menu_block_out_going);
		blockCoolSms= (RelativeLayout) view.findViewById(R.id.menu_cool_sms);
		blockIncoming.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				retain.homeListener.addListBlockIncoming();
			}
		});
		blockOutgoing.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				retain.homeListener.addListBlockOutgoing();
			}
		});
		
		blockSms.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				retain.homeListener.addListBlock();
			}
		});
		blockCoolSms.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				retain.homeListener.addListCoolSms();
			}
		});
	}


	public static MenuFacebookFragment newInstance(MenuFacebookRetain retainFragment) {
		MenuFacebookFragment menuFacebookFragment = new MenuFacebookFragment();

		Bundle bundle = new Bundle();
		bundle.putParcelable(ARGUMENTS, retainFragment);
		menuFacebookFragment.setArguments(bundle);

		return menuFacebookFragment;
	}

	public class FillUsernameReceiver extends BroadcastReceiver {
		public static final String	ACTION_FILL	= "vcc.com.muachung.fillusername";

		@Override
		public void onReceive(Context context, Intent intent) {
		}
	}
}

class MenuFacebookRetain implements Parcelable {
	protected HomeListener homeListener;
	
	public MenuFacebookRetain(HomeListener homeListener) {
		this.homeListener = homeListener;
	}

	public MenuFacebookRetain(Parcel p) {
		readFromParcel(p);
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	private void readFromParcel(Parcel p) {
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeParcelable(homeListener, flags);

	}

	@SuppressWarnings("rawtypes")
	public static final Parcelable.Creator	CREATOR	= new Parcelable.Creator() {
														public MenuFacebookRetain createFromParcel(Parcel in) {
															return new MenuFacebookRetain(in);
														}

														public MenuFacebookRetain[] newArray(int size) {
															return new MenuFacebookRetain[size];
														}
													};
}
